#include "stdafx.h"
#include "Ray.h"

using namespace mmath;

float Ray::intersectSphere(XMVECTOR spherePos, float sphereRadius) const
{
	XMVECTOR rayOrig = XMLoadFloat3(&Origin);
	XMVECTOR rayDir = XMLoadFloat3(&Direction);

	XMVECTOR dist = XMVectorSubtract(rayOrig, spherePos);

	float B = XMVectorGetX(XMVector3Dot(dist, rayDir));
	float C = XMVectorGetX(XMVector3Dot(dist, dist)) - sphereRadius * sphereRadius;
	float delta = B*B - C;

	if(delta < 0.0f)
		return std::numeric_limits<float>::infinity();

	return -B - sqrtf(delta);
}